module.exports = {
    computed: {
        hasLogin() {
            // console.log(this.$store.getters)
            if (this.$store.getters.user === undefined ||　!this.$store.getters.user) {
                return false
            }
            return this.$store.getters.user.user_code
        }
    },
    methods: {
        copyText(text) {
            uni.setClipboardData({
                data: text,
                showToast: false,
                success: () => {
                    this.$u.toast('已复制到剪贴板');
                },
                fail: () => {
                    this.$u.toast('复制失败');
                }
            })
        },
        goToLoginPage() {
            uni.navigateTo({
                url: "/pages/login/index"
            })
        },
        gotoPath(path, auth = true, param = null, jump_type = 1) {
            // 如果需要授权且用户未登录，则提示用户登录
            if (auth && !this.hasLogin) {
                this.$u.toast('请登录后查看');
                return;
            }
            // 如果路径为空，则提示暂未开放
            if (!path) {
                this.$u.toast('暂未开放');
                return;
            }
            // 如果提供了参数param，则将其拼接到path中
            if (param) {
                path += (path.indexOf('?') === -1 ? '?' : '&') + this.serializeParam(param);
            }
            if (path.indexOf('http') === 0) {
                uni.navigateTo({
                    url: '/pages/packageA/h5/web?url=' + path,
                    success: () => {
                    },
                    fail: (err) => {
                        console.log(err)
                    }
                })
                return;
            }
            // 跳转方式，1=内页，2=tab底部导航，3=外链
            if (jump_type == 3) {
                location.href = path;
            } else if (jump_type == 2) {
                uni.switchTab({
                    url: path
                });
            } else {
                uni.navigateTo({
                    url: path
                });
            }


        },
        serializeParam(param) {
            if (!param) {
                return '';
            }
            // 将参数对象序列化为URL查询字符串
            const params = [];
            for (const key in param) {
                if (param.hasOwnProperty(key)) {
                    params.push(`${encodeURIComponent(key)}=${encodeURIComponent(param[key])}`);
                }
            }
            return params.join('&');
        },


        /**
         * 2022-10-31 重写setData
         * 2023-05-08 增加微信“简易双向绑定”支持
         * 用于转换后的uniapp的项目能直接使用this.setData()函数
         * @param {Object} obj
         * @param {Object} callback
         */
        setData(obj, callback = null) {
            /**
             * 变量名正则
             */
            const variableNameReg = /^([^\x00-\xff]|[a-zA-Z_$])([^\x00-\xff]|[a-zA-Z0-9_$])*$/
            Object.keys(obj).forEach((key) => {
                this._set(this, key, obj[key])

                //处理微信“简易双向绑定”
                if (variableNameReg.test(key) && key.endsWith("Clone")) {
                    let propName = key.replace(/Clone$/, "")
                    if (this.$options && this.$options.propsData[propName]) {
                        this.$emit(`update:${propName}`, obj[key])
                    }
                }
            })

            this.$forceUpdate();
            if (typeof callback == 'function') this.$nextTick(callback)
        },
        _set(obj, path, value) {
            if (Object(obj) !== obj) return obj // When obj is not an object
            // If not yet an array, get the keys from the string-path
            if (!Array.isArray(path)) path = path.toString().match(/[^.[\]]+/g) || []
            path.slice(0, -1).reduce((a, c, i) => // Iterate all of them except the last one
                    Object(a[c]) === a[c] // Does the key exist and is its value an object?
                        // Yes: then follow that path
                        ?
                        a[c]
                        // No: create the key. Is the next key a potential array-index?
                        :
                        a[c] = Math.abs(path[i + 1]) >> 0 === +path[i + 1] ? [] // Yes: assign a new array object
                            :
                            {}, // No: assign a new plain object
                obj)[path[path.length - 1]] = value // Finally assign the value to the last key
            return obj // Return the top-level object to allow chaining
        },
        //消息提醒
        show_msg(msg, type = 'none', duration = 1500, callback) {
            uni.showToast({
                title: msg,
                icon: type,
                duration: duration,
                success: () => {
                },
                complete: () => {
                }
            })
            if (typeof callback == 'function') {
                setTimeout(() => {
                    callback()
                }, duration)
            }
        }

    }

}
